#!/bin/sh
PROG=`basename $0`

usage() {
	echo "Usage: $PROG [-Fc] file"
	echo "$PROG converts text files from the common PC database export format"
	echo '(e.g. "Name","Address1","Address2","Address3",....) into something'
	echo "that UNIX utilities like sort(1), cut(1) etc and Screen Manager can"
	echo "manipulate (e.g. Name;Address1;Address2;...)"
	echo
	echo "It first checks that the proposed separator 'c' is not already in use"
	echo "in the file. The new file appears on STDOUT. The default separator"
	echo "is 'TAB'. Don't use '*' as a separator - it breaks getopt."
	exit 1;
}

SEPARATOR='	' # (TAB)

set -- `getopt F: $*`
if [ $? != 0 ]
then
	usage >&2
fi

for i in $*
do
	case $i in
	-F) SEPARATOR=$2; shift 2;;
	--) shift; break;;
	esac
done

if [ -z "$1" ]
then
	usage >&2
fi

FILE=$1 

fgrep "$SEPARATOR" $1 >/dev/null
if [ $? -eq 0 ]
then
	echo "The proposed separator ('$SEPARATOR') already appears in the data!" >&2
	exit 2
fi

sed -e "s/^\"//
s/\"[ 	]*$//
s/\"[ 	]*,[ 	]*\"/$SEPARATOR/g" $FILE